define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            Table.api.init({
                extend: {
                    index_url: 'scanwork/stock/index' + location.search,
                    table: 'scanwork_stock',
                }
            });

            var table = $("#table");

            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: 'ID'},
                        {field: 'material.material_code', title: '物料编码', operate: 'LIKE'},
                        {field: 'material.material_name', title: '物料名称', operate: 'LIKE'},
                        {field: 'warehouse.warehouse_name', title: '仓库'},
                        {field: 'location.location_code', title: '库位'},
                        {field: 'batch_no', title: '批次号'},
                        {field: 'quantity', title: '库存数量', sortable: true},
                        {field: 'available_quantity', title: '可用数量'},
                        {field: 'locked_quantity', title: '锁定数量'},
                        {field: 'unit', title: '单位'},
                        {field: 'quality_status', title: '质量状态', searchList: {1:'合格',2:'待检',3:'不合格',4:'让步接收'}},
                        {field: 'last_in_time', title: '最后入库', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            Table.api.bindevent(table);
        },
        alert: function () {
            $.ajax({
                url: 'scanwork/stock/alert',
                dataType: 'json',
                success: function(ret) {
                    if (ret.code === 1 && ret.data) {
                        var html = '<thead><tr><th>物料</th><th>仓库</th><th>当前库存</th><th>预警类型</th><th>预警内容</th></tr></thead><tbody>';
                        $.each(ret.data, function(i, row) {
                            html += '<tr>';
                            html += '<td>' + row.material_name + '</td>';
                            html += '<td>' + (row.warehouse_name || '全部') + '</td>';
                            html += '<td>' + row.current_quantity + '</td>';
                            html += '<td>' + (row.alert_type == 1 ? '低库存' : row.alert_type == 2 ? '高库存' : row.alert_type == 3 ? '即将过期' : '呆滞') + '</td>';
                            html += '<td>' + row.alert_content + '</td>';
                            html += '</tr>';
                        });
                        html += '</tbody>';
                        $('#alert-table').html(html);
                    }
                }
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});

